//
//  SEGActivityLotteryTurnTableCell.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/16.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGActivityLotteryTurnTableCell.h"
#import "SEGActivityLotteryRotateView.h"

@interface SEGActivityLotteryTurnTableCell ()

@property (nonatomic, strong) UIButton* restCountButton; //剩余次数
@property (nonatomic, strong) UIImageView* bgImageView; //抽奖背景
/**
 抽奖转盘
 */
@property (nonatomic, strong)SEGActivityLotteryRotateView *rotateView;

@end

@implementation SEGActivityLotteryTurnTableCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.backgroundColor = [UIColor colorWithRed:79/255.0 green:211/255.0 blue:185/255.0 alpha:1];
    
    self.bgImageView.hidden = NO;
    self.restCountButton.hidden = NO;
    
    SEGAdjustableButton *winRecordButton = [[SEGAdjustableButton alloc] init];
    [winRecordButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"img_activity01_gift")] forState:UIControlStateNormal];
    //    [winRecordButton setTitle:@"我的中奖记录 >" forState:UIControlStateNormal];
    NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc] initWithString:@"我的中奖记录 >"];
    NSRange strRange2 = {0,[str2 length]};
    [str2 addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange2];
    [str2 addAttribute:NSForegroundColorAttributeName value:UIColor.whiteColor range:strRange2];
    [winRecordButton setAttributedTitle:str2 forState:UIControlStateNormal];
    
    [winRecordButton setTitleColor:kColor_W forState:UIControlStateNormal];
    winRecordButton.titleLabel.font = kFont_13;
    [winRecordButton addTarget:self action:@selector(seg_winRecordButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:winRecordButton];
    [winRecordButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(self.bgImageView.mas_bottom).offset(29).priorityHigh();
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-29);
    }];
    [winRecordButton layoutButtonWithEdgeInsetsStyle:SEGButtonEdgeInsetsStyleLeft imageTitleSpace:6];

    [self.contentView mas_makeConstraints:^(MASConstraintMaker* make) {
        // iOS10 以上的适配
        if ([[[UIDevice currentDevice] systemVersion] floatValue] < 10.0) {
            make.left.top.right.equalTo(self);
            make.bottom.equalTo(winRecordButton.mas_bottom).mas_offset(29);
        }else{
            make.edges.equalTo(self);
        }
    }];
}

- (void)setUpCellWithLotteryDetailModel:(SEGActivityLotteryDetailModel *)model actStatus:(SEGActivityStatus)actStatus
{
    if (actStatus == SEGActivityStatus_Over) {
        [self.restCountButton setTitle:@"  亲，活动已经结束啦！ " forState:UIControlStateNormal];
    }else if([model.freeLotterys intValue] > 0) {
        [self.restCountButton setTitle:[NSString stringWithFormat:@"  您今天有 %d 次免费抽奖机会  ",[model.freeLotterys intValue]] forState:UIControlStateNormal];
    }else if([model.lotteryTimes intValue] > 0){
        [self.restCountButton setTitle:[NSString stringWithFormat:@"  您今天还有 %d 次额外抽奖的机会，%d 积分每次。 ",[model.lotteryTimes intValue], [model.price intValue]] forState:UIControlStateNormal];
    }else {
        [self.restCountButton setTitle:@"  亲，您的抽奖机会已经用完！ " forState:UIControlStateNormal];
    }
    
    if (!self.rotateView) {
        /*4.6.0 添加谢谢参与的缺省图链接*/
        
        self.rotateView = [[SEGActivityLotteryRotateView alloc] initWithFrame:CGRectMake((LYScreenWidth() - 340 * LYScreenScaleWidth()) / 2.0, 65, 340 * LYScreenScaleWidth(), 340 * LYScreenScaleWidth()) dataSource:model.prizes placehoder:model.notLotteryPic finished:^(SEGActivityLotteryPrizeModel* result , BOOL isClickButton) {
            if (isClickButton) {//点击 请求抽奖接口
                if (self.delegate && [self.delegate respondsToSelector:@selector(lotteryBegin)]) {
                    [self.delegate lotteryBegin];
                }
            }else {//展示抽奖结果弹窗
                if (self.delegate && [self.delegate respondsToSelector:@selector(showLotteryResult)]) {
                    [self.delegate showLotteryResult];
                }
            }
        }];
        
        [self.bgImageView addSubview:self.rotateView];
    }else {
        [self.rotateView stopAtMiddle];
    }
}

- (void)startAnimationWithPrizeId:(NSString *)prizeId
{
    [self.rotateView startAnimation:prizeId];
}

- (void)seg_winRecordButtonPressed
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(goToWinRecordViewController)]) {
        [self.delegate goToWinRecordViewController];
    }
}

#pragma mark - getter
- (UIButton *)restCountButton
{
    if (!_restCountButton) {//img_activity01_newbg
        _restCountButton = [[UIButton alloc] init];
        [_restCountButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        _restCountButton.titleLabel.font = kFont_13;
        
        UIImage* image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_activity_cishubg")];
        CGFloat height = image.size.height;
        CGFloat width = image.size.width;
        UIEdgeInsets insets = UIEdgeInsetsMake(height / 2, width / 2, height / 2, width / 2);
        UIImage *image2 = [[UIImage imageWithContentsOfFile:SEGPicName(@"img_activity_cishubg")] resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
        [_restCountButton setBackgroundImage:image2 forState:UIControlStateNormal];
        [self.contentView addSubview:_restCountButton];
        [self.contentView bringSubviewToFront:_restCountButton];
        [_restCountButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.contentView);
            make.top.equalTo(self.contentView).offset(15);
//            make.width.mas_equalTo(159);
            make.height.mas_equalTo(30);
        }];
    }
    return _restCountButton;
}

- (UIImageView *)bgImageView
{
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] init];
        _bgImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_activity01_bg")];
        [self.contentView addSubview:_bgImageView];
        [_bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView);
            make.centerX.equalTo(self);
            make.width.mas_equalTo(LYScreenWidth());
            make.height.mas_equalTo(405 * LYScreenScaleWidth());
        }];
        _bgImageView.userInteractionEnabled = YES;
    }
    return _bgImageView;
}

@end
